package com.fingard.dsp.bank.directbank.jsjnrcb01;

import java.util.Date;
import java.util.Map;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.dsp.bank.directbank.jhbank01.JHBANK01Header;
import com.fingard.dsp.bank.directbank.jhbank01.utils.PublickeyEncript;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpPortClient;
import com.fingard.net.TcpReceiver;
import com.fingard.net.WebRequest;
import com.fingard.util.TransIDHelper;
import com.fingard.xml.XmlTextWriter;

public class JSJNRCB01Base extends DirectBase implements ITcpClient{
	
	protected static TransIDHelper idHelper = new TransIDHelper(1,9999);
	
    @Override
    public void getContentLength(TcpReceiver p_receiver) throws Exception {
        if (p_receiver.contentLength == -1) {
            if (p_receiver.readedLength >= 6) {
              String tmpHead = new String(p_receiver.recBytes, 0, 6, getCharset());
                p_receiver.contentLength = Integer.parseInt(tmpHead.trim());
                p_receiver.contentStartIndex =6;
            }
        }
    }
    protected String getCharset(){
        return "UTF-8";
    }

    //tcp格式
    protected String[] sendToBank(String xml) throws Exception {
        xml = String.format("%08d%s", xml.length(), xml);
        WriteBankLogLn("发送给银行的报文明文："+xml);
        WriteBankUrlLogLn(bankFront.getIpPortOrUrl());
        String[] result = {"",""};
        TcpPortClient client = new TcpPortClient(bankFront.serverIP,Integer.valueOf(bankFront.serverPort),this);
        TcpReceiver receiver =client.sendMessage(xml.getBytes(getCharset()));
        result[0] = receiver.connState;
        if(result[0].length()!=0){
           result[1]=receiver.connMsg;
            return result;
        }

        result[1] = new String(receiver.recBytes,0,receiver.contentLength+receiver.contentStartIndex,getCharset());
        WriteBankLogStep3(result[1]);
        return result;
    }
    
    //http格式
    protected String[] sendToBank(String pReqStr,String opName) throws Exception{
    	String xml = String.format("%08d%s", pReqStr.length(), pReqStr);
    	WriteBankLogStep2(xml);
        String[] retStr = new String[]{"",""};
        String tmpAddrUrl = "http://" + bankFront.serverIP + ":" + bankFront.serverPort+"/eweb/" + opName + ".do?usrID="+
                bankFront.userName+"&userPassword=" + bankFront.loginPasswd + "&operatorID="+bankFront.operator+"&SIGDATA=1";
        String sendInfo = xml;
        WriteBankLogLn("2.提交银行的地址：" + tmpAddrUrl);
        WebRequest tmpWebRequest = new WebRequest(tmpAddrUrl);
        tmpWebRequest.setConnectTimeout(10000);
        tmpWebRequest.setRequestProperty("User-Agent", "API");

        byte[] tmpUpBytes = sendInfo.getBytes(getCharset());
        retStr = tmpWebRequest.upload(tmpUpBytes, getCharset());

        WriteBankLogStep3(retStr[1]);
        return retStr;
    }

    protected void writeCommenHeader(XmlTextWriter xmlTextWriter,JSJNRCB01Header header){
        xmlTextWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\""+getCharset()+"\" ?>");
        xmlTextWriter.writeStartElement("Message");
        xmlTextWriter.writeStartElement("Head");
        xmlTextWriter.writeElementString("TransCode ",header.TransCode);//操作号
        xmlTextWriter.writeElementString("ERPTrsTimestamp",Format.DateTimeFormat.format(new Date()));//柜员
        xmlTextWriter.writeElementString("ERPJnlNo",getERPJnlNo());//渠道
        xmlTextWriter.writeEndElement();
    }

    protected String getERPJnlNo(){
		return Format.DateTime12Year2Format.format(new Date())+idHelper.getNextIDStr();
	}
}
